package com.dcms.admin.controller;

import com.dcms.admin.bean.Comment;
import com.dcms.admin.dao.CommentMapper;
import com.dcms.admin.service.ICommentService;
import com.dcms.util.IPUtil;
import com.dcms.util.LayTableBean;
import com.dcms.util.ResultBean;
import com.dcms.util.StringUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * \* 用于留言表的处理
 * \* @name: CommentController
 * \* @author: 武佳伟丶
 * \* @date: 2018/2/11 0011
 * \* @tine: 10:51
 * \* @description: To change this template use File | Settings | File Templates.
 * \
 */
@Controller
@RequestMapping("admin/comment")
public class CommentController extends BaseController {

    @Autowired
    private ICommentService commentService;

    @RequestMapping("list")
    public String list(){
        return "admin/comment/list";
    }
    
    /**
     * @author: 武佳伟丶
     * @description: 查询数据
     * @date: 10:55 2018/2/11 0011
     * @param: [request, param, limit, page]
     * @return: com.dcms.util.LayTableBean<com.dcms.admin.bean.Comment>
     */
    @RequestMapping("query")
    @ResponseBody
    public LayTableBean<Comment> query(
            HttpServletRequest request,
            String param,
            Comment record,
            Integer limit,
            Integer page
    ){
        page = page == null ? 1 : page;
        limit = limit == null ? 12 : limit;

        PageHelper.startPage(page, limit);
        List<Comment> list = commentService.selectAll(param, record);
        PageInfo<Comment> info = new PageInfo<Comment>(list);
        LayTableBean<Comment> bean = new LayTableBean<Comment>(info.getList(), (int)info.getTotal());
        return bean;
    }

    /**
     * @author: 武佳伟丶 
     * @description: 删除，支持批量
     * @date: 14:39 2018/2/12 0012
     * @param: [id]
     * @return: com.dcms.util.ResultBean
     */
    @RequestMapping("delete")
    @ResponseBody
    public ResultBean deleteBatch(@RequestParam(value = "id[]", required = true)Integer[] id){
        int rows = commentService.deleteBatch(id);
        if (rows > 0){
            return new ResultBean();
        }
        return new ResultBean(10021);
    }

    /**
     * @author: 武佳伟丶 
     * @description: 更新
     * @date: 18:55 2018/2/13 0013
     * @param: [record]
     * @return: com.dcms.util.ResultBean
     */
    @RequestMapping(value = "update", method = RequestMethod.POST)
    @ResponseBody
    public ResultBean update(Comment record){
        int rows = commentService.updateByPrimaryKeySelective(record);
        if (rows > 0){
            return new ResultBean();
        }
        return new ResultBean(10021);
    }

    /**
     * @author: 武佳伟丶 
     * @description: 跳转到数据编辑页，用于新增数据
     * @date: 14:42 2018/2/18 0018
     * @param: [commentId, request]
     * @return: java.lang.String
     */
    @RequestMapping("toinsert")
    public String toInsert(){
        return "admin/comment/edit";
    }
    
    /**
     * @author: 武佳伟丶
     * @description: 录入数据
     * @date: 14:52 2018/2/18 0018
     * @param: [record]
     * @return: com.dcms.util.ResultBean
     */
    @RequestMapping("insert")
    @ResponseBody
    public ResultBean insert(Comment record, String top, HttpServletRequest request){
        record.setOrders(1);
        if (StringUtil.isNotEmpty(top)){
            if ("on".equals(top)){
                record.setOrders(99);
            }
        }
        record.setCtime(new Date());
        record.setDeleted(1);
        record.setIp(IPUtil.getIpAddr(request));
        int rows = commentService.insertSelective(record);
        if (rows > 0){
            return  new ResultBean();
        }
        return new ResultBean();
    }
}